package com.amado.course36ByteStream;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
/**
 * GBK编码一个中文占两个字节
 * UTF-8编码一个中文占三个字节
 * 英文不分编码都占一个字节
 * @author Administrator
 *
 */
public class S00StringBytes {
	public static void main(String[] args) {
		//JAVA虚拟机中默认的字符集
		System.out.println(Charset.defaultCharset());
		printBytes("abc");
		printBytes("中");
		printBytes("abc","GBK");
		printBytes("中","GBK");
	}
	
	private static void printBytes(String value){
		byte[] bytes = value.getBytes();
		System.out.print(value +"\t长度等于"+bytes.length+" value 等于");
		for(byte temp:bytes){
			System.out.print(temp+ " ");
		}
		System.out.println();
	}
	private static void printBytes(String value,String charset){
		byte[] bytes;
		try {
			bytes = value.getBytes(charset);
			System.out.print(value +"\t长度等于"+bytes.length+" value 等于");
			for(byte temp:bytes){
				System.out.print(temp+ " ");
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		System.out.println();
	}
}
